﻿var m_Image;
var m_LBoardCell;
var m_lPiece;
var m_lDeadPiece;
var m_chosenPiece;
var players;
function Board()
{
    m_Image = new Image();

    //danh sach cac o trong ban co
    m_LBoardCell = new Array(BoardInfo.row());
    for (var i = 0; i < BoardInfo.row() ; ++i)
    {
        m_LBoardCell[i] = new Array(BoardInfo.col());
    }

    //danh sac cac quan co
    m_lPiece = new Array(BoardInfo.pieceEachSide() * BoardInfo.sideCount());

    m_lDeadPiece = new Array();
    players = new Player();
}

Board.prototype.InitializeChessPiece = function()
{
    for (var indexX = 0; indexX < BoardInfo.row(); ++indexX)
    {
        for (var indexY = 0; indexY < BoardInfo.col(); ++indexY)
        {
            m_LBoardCell[indexX][indexY] = null;
        }
    }

    for (var x = 0; x < (BoardInfo.pieceEachSide() * BoardInfo.sideCount()); ++x)
    {
        m_lPiece[x] = null;
    }

    //Initialize the red side piece
    var row = 0, col = 0, index = 0;

    //Draw tower
    //row = 0, collumn = 0
    m_lPiece[index] = new ChariotPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_ju.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_ju_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw knigth
    //row = 0, collumn = 1
    col++;
    index++;
    m_lPiece[index] = new HorsePiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_ma.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_ma_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw elephant
    //row = 0, collumn = 2
    col++;
    index++;
    m_lPiece[index] = new ElephantPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_xiang.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_xiang_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw general bodyguard
    //row = 0, collumn = 3
    col++;
    index++;
    m_lPiece[index] = new AdvisorPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_shi.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_shi_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw general
    //row = 0, collumn = 4
    col++;
    index++;
    m_lPiece[index] = new GeneralPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_gong.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_gong_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];
    BasicRule.SetRedKing(m_lPiece[index]);

    //Draw general bodyguard
    //row = 0, collumn = 5
    col++;
    index++;
    m_lPiece[index] = new AdvisorPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_shi.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_shi_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw elephant
    //row = 0, collumn = 6
    col++;
    index++;
    m_lPiece[index] = new ElephantPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_xiang.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_xiang_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw knight
    //row = 0, collumn = 7
    col++;
    index++;
    m_lPiece[index] = new HorsePiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_ma.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_ma_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw tower
    //row = 0, collumn = 8
    col++;
    index++;
    m_lPiece[index] = new ChariotPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_ju.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_ju_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw artillery
    //row = 2, collumn =1
    row = 2, col = 1;
    index++;
    m_lPiece[index] = new CannonPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_pao.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_pao_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row = 2, collumn = 7
    col = 7;
    index++;
    m_lPiece[index] = new CannonPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_pao.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_pao_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw spawn
    //row =3, collumn = 0
    row = 3, col = 0;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =3, collumn = 2
    col = 2;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =3, collumn = 4
    col = 4;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =3, collumn = 6
    col = 6;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =3, collumn = 8
    col = 8;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "r_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "r_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(true);
    m_LBoardCell[row][col] = m_lPiece[index];

    /*
	 *
	 *
	 */
    //Initialize the blue side piece
    row = BoardInfo.row() - 1, col = 0;
    index++;

    //Draw tower
    //row = 0, collumn = 0
    m_lPiece[index] = new ChariotPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_ju.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_ju_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw knight
    //row = 0, collumn = 1
    col++;
    index++;
    m_lPiece[index] = new HorsePiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_ma.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_ma_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw elephant
    //row = 0, collumn = 2
    col++;
    index++;
    m_lPiece[index] = new ElephantPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_xiang.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_xiang_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw general bodyguard
    //row = 0, collumn = 3
    col++;
    index++;
    m_lPiece[index] = new AdvisorPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_shi.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_shi_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw general
    //row = 0, collumn = 4
    col++;
    index++;
    m_lPiece[index] = new GeneralPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_gong.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_gong_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];
    BasicRule.SetBlackKing(m_lPiece[index]);

    //Draw general bodyguard
    //row = 0, collumn = 5
    col++;
    index++;
    m_lPiece[index] = new AdvisorPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_shi.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_shi_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw elephant
    //row = 0, collumn = 6
    col++;
    index++;
    m_lPiece[index] = new ElephantPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_xiang.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_xiang_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw knight
    //row = 0, collumn = 7
    col++;
    index++;
    m_lPiece[index] = new HorsePiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_ma.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_ma_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw tower
    //row = 0, collumn = 8
    col++;
    index++;
    m_lPiece[index] = new ChariotPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_ju.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_ju_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw artillery
    //row = 7, collumn = 1
    row = 7, col = 1;
    index++;
    m_lPiece[index] = new CannonPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_pao.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_pao_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row = 7, collumn = 7
    col = 7;
    index++;
    m_lPiece[index] = new CannonPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_pao.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_pao_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //Draw spawn
    //row =3, collumn = 0
    row = 6, col = 0;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =6, collumn = 2
    col = 2;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =6, collumn = 4
    col = 4;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =6, collumn = 6
    col = 6;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];

    //row =6, collumn = 8
    col = 8;
    index++;
    m_lPiece[index] = new SoliderPiece();
    m_lPiece[index].SetImage(ConstValue.pixDir() + "b_zu.png");
    m_lPiece[index].SetSelectedImage(ConstValue.pixDir() + "b_zu_h.png");
    m_lPiece[index].SetRow(row);
    m_lPiece[index].SetCollumn(col);
    m_lPiece[index].SetRedSide(false);
    m_LBoardCell[row][col] = m_lPiece[index];
}

function IsLegitClick(pos) {
    var x = pos.GetX() - ConstValue.xBeginDraw() - ConstValue.errorX();
    var y = pos.GetY() - ConstValue.yBeginDraw() + ConstValue.errorY();

    if ((0 < x) && (0 < y)) {
        var col = Math.floor(y / ConstValue.cellHeightWidth());
        var row = Math.floor(x / ConstValue.cellHeightWidth());

        if ((col < BoardInfo.col()) && (row < BoardInfo.row())) {            
            return true;
        }
    }

    return false;
}

Board.prototype.SetBoard = function(boardDir)
{
    m_Image.src = boardDir;
}

Board.prototype.Draw = function()
{
    var context = canvas.getContext("2d");
    context.drawImage(m_Image, ConstValue.xBeginDraw(), ConstValue.yBeginDraw());
    for (var x = 0; x < (BoardInfo.pieceEachSide() * BoardInfo.sideCount()); ++x)
    {
        if (true == m_lPiece[x].IsActive())
        {   
            m_lPiece[x].Update();
            m_lPiece[x].Draw();
        }
    }
}

function MovePiece(position)
{
    //if (true == BasicRule.IsKingFaceKing() || true == BasicRule.IsKingBeingChecked(players.GetCurrentTurn())) {
    //    ////Check if there is no next move exist
    //    //if (false == DoesNextMoveExist()) {
    //    //    m_gameState = false;
    //    //    return;
    //    //}

    //    //Notice for player
        
    //}

    var bCanMove = false;
    var pointOldPosition = new Point(0, 0);
			
    //If chosen piece is null and there is a piece at the chosen position, set chosen piece to the piece at that position 
    //and then change that piece's state    
    if (null == m_chosenPiece)
    {
        if ((null != (m_LBoardCell[position.GetX()][position.GetY()])) &&
           m_LBoardCell[position.GetX()][position.GetY()].IsRedSide() == players.GetCurrentTurn())
        {
            m_chosenPiece = m_LBoardCell[position.GetX()][position.GetY()];
            m_chosenPiece.ChangeSelectedState();
            soundManager.s_select.play();
        }
    }
    else
    {
        //If the chosen piece is chosen again, change selected state and point chosenPiece to null
        if (m_chosenPiece.GetCollumn() == position.GetY() && m_chosenPiece.GetRow() == position.GetX())
        {
            m_chosenPiece.ChangeSelectedState();
            m_chosenPiece = null;
            soundManager.s_select.play();
        }
        else
        {
            pointOldPosition.SetY(m_chosenPiece.GetCollumn());
            pointOldPosition.SetX(m_chosenPiece.GetRow());

            //Check if the chosen position is one of the possible move and the move is legit
            if (true == m_chosenPiece.CanMoveTo(pointOldPosition, position))
            {						
                if (null != m_LBoardCell[position.GetX()][position.GetY()] )
                {
                    if (m_LBoardCell[position.GetX()][position.GetY()].IsRedSide() != m_chosenPiece.IsRedSide())                        
                            bCanMove = true;
                }
                else 
                {                    
                    bCanMove = true;
                }
            }           
        }
    }

    if (true == bCanMove)
    {
        //Point to the chosen position
        var cpCapturedPiece = m_LBoardCell[position.GetX()][position.GetY()];

        //If there is a piece at the chosen position, change piece active state at the chosen position
        if(null != m_LBoardCell[position.GetX()][position.GetY()])
        {
            m_LBoardCell[position.GetX()][position.GetY()].SetDeactive();
        }

        //Get both side king
        //
        //Point board new position to the piece address
        m_LBoardCell[position.GetX()][position.GetY()] = m_LBoardCell[m_chosenPiece.GetRow()][m_chosenPiece.GetCollumn()];
        //Set board old position to null
        m_LBoardCell[m_chosenPiece.GetRow()][m_chosenPiece.GetCollumn()] = null;

        //Assign current chosen piece position to the pointOldPosition
        //Assign new position to the current chosen piece's position
        m_chosenPiece.SetCollumn(position.GetY());
        m_chosenPiece.SetRow(position.GetX());

        //If game rule make this move invalid
        if (true == BasicRule.IsKingFaceKing() || true == BasicRule.IsKingBeingChecked(players.GetCurrentTurn()))
        {
            //Point board old position to the piece address
            m_LBoardCell[pointOldPosition.GetX()][pointOldPosition.GetY()] = m_LBoardCell[position.GetX()][position.GetY()];
            m_chosenPiece.SetCollumn(pointOldPosition.GetY());
            m_chosenPiece.SetRow(pointOldPosition.GetX());

            if (null != cpCapturedPiece)
            {
                m_LBoardCell[position.GetX()][position.GetY()] = cpCapturedPiece;
                cpCapturedPiece.SetActive();
            }
            else
            {
                m_LBoardCell[position.GetX()][position.GetY()] = cpCapturedPiece;
                cpCapturedPiece.SetDeactive();
            }
        }
        else
        {
            soundManager.s_move.play();
			players.ChangePlayer();
        }

        //Unchosen the current chosen piece
        if (true == m_chosenPiece.IsSelected())
        {
            m_chosenPiece.ChangeSelectedState();
        }

        m_chosenPiece = null;

        //Check checkmate!
        if (true == BasicRule.IsKingFaceKing() || true == BasicRule.IsKingBeingChecked(players.GetCurrentTurn())) {
            //Notice for player
            if (true == players.GetCurrentTurn())
            {
                $('#thongBao').html('Quân Đỏ Bị Chiếu Tướng');
                $('#thongBao').fadeIn(1000).delay(1000).fadeOut(1000);
            }
            else
            {
                $('#thongBao').html('Quân Xanh Bị Chiếu Tướng');
                $('#thongBao').fadeIn(1000).delay(1000).fadeOut(1000);
            }
        }
    }
}

Board.prototype.Click = function (pos) {
    if (m_gameState == false)
    {
        return;
    }

    if (true == IsLegitClick(pos)) {
        var boardPos = new Point(0, 0);
        boardPos.SetX(Math.floor((pos.GetX() - ConstValue.xBeginDraw() - ConstValue.errorX()) /
            ConstValue.cellHeightWidth()));
        boardPos.SetY(Math.floor((pos.GetY() - ConstValue.yBeginDraw() + ConstValue.errorY()) /
            ConstValue.cellHeightWidth()));
        MovePiece(boardPos);
    }
}

function DoesNextMoveExist()
{
    for (var index = 0; index < BoardInfo.pieceEachSide() * BoardInfo.sideCount(); ++index)
    {
        if (m_lPiece[index].IsRedSide() == players.GetCurrentTurn())
        {
            var posList = [];
            posList = m_lPiece[index].GetPotentialNextPositions(posList, new Point(m_lPiece[index].GetRow(), m_lPiece[index].GetCollumn()));

            for (var i = 0; i < posList.length; ++i)
            {
                if (posList[i].GetX() < 0 || posList[i].GetY() < 0 ||
                    posList[i].GetX() >= BoardInfo.row() || posList[i].GetY() >= BoardInfo.col())
                {
                    continue;
                }
                var pointOldPosition = new Point(m_lPiece[index].GetRow(), m_lPiece[index].GetCollumn());
                var bCanMove = false;

                //Check if the chosen position is one of the possible move and the move is legit
                if ((null != m_LBoardCell[posList[i].GetX()][posList[i].GetY()]) &&
                    (true == m_LBoardCell[posList[i].GetX()][posList[i].GetY()].CanMoveTo(pointOldPosition, posList[i])))
                {
                    if (null != m_LBoardCell[posList[i].GetX()][posList[i].GetY()])
                    {
                        if (m_LBoardCell[posList[i].GetX()][posList[i].GetY()].IsRedSide() != players.GetCurrentTurn())
                        {
                            bCanMove = true;
                        }                                
                    }
                    else
                    {
                        bCanMove = true;
                    }
                }

                if (true == bCanMove)
                {
                    //Point to the chosen position
                    var cpCapturedPiece = m_LBoardCell[posList[i].GetX()][posList[i].GetY()];

                    //If there is a piece at the chosen position, change piece active state at the chosen position
                    if (null != m_LBoardCell[posList[i].GetX()][posList[i].GetY()])
                    {
                        m_LBoardCell[posList[i].GetX()][posList[i].GetY()].SetDeactive();
                    }

                    //Get both side king
                    //
                    //Point board new position to the piece address
                    m_LBoardCell[posList[i].GetX()][posList[i].GetY()] = m_LBoardCell[pointOldPosition.GetX()][pointOldPosition.GetY()];
                    //Set board old position to null
                    m_LBoardCell[pointOldPosition.GetX()][pointOldPosition.GetY()] = null;

                    //If game rule make this move invalid
                    if (true == BasicRule.IsKingFaceKing() || true == BasicRule.IsKingBeingChecked(players.GetCurrentTurn()))
                    {
                        //Point board old position to the piece address
                        m_LBoardCell[pointOldPosition.GetX()][pointOldPosition.GetY()] = m_LBoardCell[posList[i].GetX()][posList[i].GetY()];

                        if (null != cpCapturedPiece)
                        {
                            m_LBoardCell[posList[i].GetX()][posList[i].GetY()] = cpCapturedPiece;
                            cpCapturedPiece.SetActive();
                        }
                        else
                        {
                            m_LBoardCell[posList[i].GetX()][posList[i].GetY()] = cpCapturedPiece;
                            cpCapturedPiece.SetDeactive();
                        }
                    }
                    else
                    {
                        //Point board old position to the piece address
                        m_LBoardCell[pointOldPosition.GetX()][pointOldPosition.GetY()] = m_LBoardCell[posList[i].GetX()][posList[i].GetY()];

                        if (null != cpCapturedPiece) {
                            m_LBoardCell[posList[i].GetX()][posList[i].GetY()] = cpCapturedPiece;
                            cpCapturedPiece.SetActive();
                        }
                        else {
                            m_LBoardCell[posList[i].GetX()][posList[i].GetY()] = cpCapturedPiece;
                            cpCapturedPiece.SetDeactive();
                        }

                        return true;
                    }
                }
            }
        }            
    }   

    return false;
}